#define _CRT_SECURE_NO_WARNINGS 1


class Solution {
public:
    bool verifyTreeOrder(vector<int>& postorder) {
        if (postorder.empty())
        {
            return true;
        }

        return verifyTreeOrderCore(postorder, 0, postorder.size() - 1);
    }

    bool verifyTreeOrderCore(vector<int>& postorder, int start, int end)
    {
        if (start >= end)
        {
            return true;
        }
        int root = postorder[end];
        int i = start;
        while (i < end && postorder[i] < root)
        {
            i++;
        }

        for (int j = i; j < end; j++)
        {
            if (postorder[j] < root)
            {
                return false;
            }
        }

        return verifyTreeOrderCore(postorder, start, i - 1) && verifyTreeOrderCore(postorder, i, end - 1);

    }
};